package com.ltt.controller;

import com.ltt.common.UserThreadLocal;
import com.ltt.dao.pojo.Question;
import com.ltt.dao.pojo.UserAnswer;
import com.ltt.dao.vo.QuestionVo;
import com.ltt.dao.vo.Result;
import com.ltt.dao.vo.params.AnswerParams;
import com.ltt.service.QuestionService;
import com.ltt.service.UserAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ‘李天泰’
 * @version 1.0
 * @description: TODO
 * @date 2023/5/3 15:54
 */
@Api(tags = "题目模块")
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserAnswerService userAnswerService;

    @ApiOperation("获取所有题目信息")
    @GetMapping("/get")
    public Result getAllQuestion() {
        List<Question> allQuestion = questionService.getAllQuestion();
        List<QuestionVo> allQuestionVo = new ArrayList<>();
        for (Question question : allQuestion) {
            QuestionVo questionVo = new QuestionVo();
            BeanUtils.copyProperties(question,questionVo);

            AnswerParams answerParams = new AnswerParams();
            answerParams.setQuestionId(question.getId());
            answerParams.setUserId(UserThreadLocal.get().getOpenid());
            UserAnswer userAnswer = userAnswerService.getUserAnswer(answerParams);

            if(userAnswer != null){
                questionVo.setIsWrite("true");
                questionVo.setIsTrue(userAnswer.getIsTrue());
                questionVo.setUserAnswer(userAnswer.getAnswer());
                questionVo.setAnswer(userAnswer.getTrueAnswer());
            }
            allQuestionVo.add(questionVo);

        }

        return Result.successWithData(allQuestionVo);
    }

}
